#!/bin/bash

PATH=/usr/bin:/bin:/usr/sbin:/sbin

LOGPATH=/ebay/bind-logs
LOG=bind-queries.log
REPORT=/var/named/tmp/top20

DATE=$(date "+%d-%b-%Y")

sleep 60

(echo -e "Total Queries"; grep "^${DATE} " $(find $LOGPATH -name ${LOG}\* -mtime -1) | wc -l | awk '{printf "%12d\n", $1}') > $REPORT


echo -e "\nTop 20 Queries" >> $REPORT
grep "^${DATE} " $(find $LOGPATH -name ${LOG}\* -mtime -1) | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 20 | awk '{printf "%12d %s\n", $1, $2}' >> $REPORT

echo -e "\nTop 20 Clients" >> $REPORT
grep "^${DATE} " $(find $LOGPATH -name ${LOG}\* -mtime -1) | sed -e 's/.* queries: client //' -e 's/#.*//' | sort | uniq -c | sort -nr | head -n 20 | \
while read -r no ip; do
	client=$(dig -x $ip +short | grep -v "^;;")
	if [ "X${client}" == "X" ]; then client="UNRESOLVED"; fi
	printf "%12d %-20s %-50s\n" $no $ip $client
done >> $REPORT

